<?php

class Controller_Publisher extends Controller_Abstract
{
	function actionShow()
	{
		$pubmodel = new Publisher();
		$publishers = $pubmodel->getAllPublishers();
		foreach ($publishers as &$p)
		{
			$subs = $pubmodel->getSubs($p['id']);
			$p['subs'] = $subs;
		}
		$this->_view['publishers'] = $publishers;
		if (isset($_SESSION['infos']))
		{
			$infos = $_SESSION['infos'];
			$this->_view['infos'] = $infos;
			unset($_SESSION['infos']);
		}
	}
	function actionAdd()
	{
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$name = $this->_context->name;
			$subName = $this->_context->sub_name;
			if (empty($name))
			{
				$errors[] = Util::getMessage(1005);
			}
			if (count($errors) == 0)
			{
				$pubmodel = new Publisher();
				$pubId = $pubmodel->add($name);
				if($pubId){
					$submodel = new Sub();
					$submodel->add($pubId, $subName);
				}
				$infos[] = Util::getMessage(3001);
				$this->_view['infos'] = $infos;
			}
			else
			{
				$this->_view['name'] = $name;
			}
		}
	}
	function actionAddsub()
	{
		$pubid = $this->_context->pubid;
		if (empty($pubid))
		{
			return $this->_redirect(url('default/index'));
		}
		$this->_view['pubid'] = $pubid;
		$pubmodel = new Publisher();
		$publisher = $pubmodel->getPublisherById($pubid);
		if ($publisher==false)
		{
			return $this->_redirect(url('default/index'));
		}
		$this->_view['publisher'] = $publisher['name'];
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$name = $this->_context->name;
			if (empty($name))
			{
				$errors[] = Util::getMessage(1005);
			}
			if (count($errors) == 0)
			{
				$pubmodel = new Publisher();
				$pubmodel->addSub($pubid, $name);
				$infos[] = Util::getMessage(3002);
				$this->_view['infos'] = $infos;
			}
			else
			{
				$this->_view['name'] = $name;
			}
		}
	}
}
?>